﻿@inherits NITASA.Views.AutofacEnabledViewPage<List<NITASA.Data.Content>>
@using NITASA.Data
@using NITASA.Services.Security
@using NITASA.Areas.Admin.Helper
@using GridMvc.Html
@{
    ViewBag.Title = "Addons";
}
    
    <div class="main-content">
        <div class="container-fluid padded">
            @if (TempData["SuccessMessage"] != null)
            {
                <div class="alert alert-success">
                    <button class="close" data-dismiss="alert" type="button">
                        ×
                    </button>
                    @TempData["SuccessMessage"]
                </div>
            }
            @if (TempData["ErrorMessage"] != null)
            {
                <div class="alert alert-error">
                    <button class="close" data-dismiss="alert" type="button">
                        ×
                    </button>
                    @TempData["ErrorMessage"]
                </div>
            }
            <div class="row-fluid">
                <div class="span12">
                    <div class="box">
                        <div class="box-header">
                            <span class="title">Browse Addons</span>
                            @if (aclService.HasRight(Rights.CreateNewAddons))
                            {
                                <ul class="box-toolbar">
                                    <li><a href="@Url.Action("Add")" title="Add New Addon"><i class="icon-plus-sign" style="margin-top: 10px"></i></a></li>
                                </ul>
                            }
                        </div>
                        <div class="box-content">
                            @if (Model != null && Model.Count() > 0)
                            {
                                @Html.Grid(Model).Columns(columns =>
                                {
                                    columns.Add(x => x.Title).Titled("Addon Title").Sortable(true).Filterable(true);
                                    columns.Add(x => x.Type).Titled("Addon Name").Sortable(true).Filterable(true);
                                    columns.Add(x => x.ContentView).Titled("Views").Sortable(true).Filterable(true);
                                    columns.Add(x => x.user.FullName).Titled("Author").Sortable(true).Filterable(true);
                                    columns.Add(c => c.isPublished).Sortable(true).Filterable(true).RenderValueAs(x => x.isPublished ? "Published" : "Draft").Titled("Type");
                                    columns.Add().SetWidth(150).RenderValueAs(x => RenderButtons(x)).Sanitized(false).Encoded(false).Titled("");
                                }).WithPaging(10, 5).WithMultipleFilters()
                            }
                            else
                            { <div class="padded">No Addons Found.</div>}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div id="modal-config" class="modal hide fade" tabindex="-1" data-focus-on="input:second">
        @using (Html.BeginForm("UpdateAddonLayout", "Addons", FormMethod.Post))
        {
            <div class="modal-header">
                <a class="close modal_Delete_icon_link" data-dismiss="modal" href=""><i class="icon-remove-sign modal_close_icon_fontsize"></i></a>                
                <button id="BtnSave" name="BtnSave" class="close modal_icon_link" type="submit">
                    <i class="icon-save modal_icon_fontsize"></i>
                </button>
                <h6 id="H14">
                    <label>Addon Layout</label>
                </h6>
            </div>
            <div class="modal-body">
                <ul class="padded separate-sections">
                    <li class="input li_input_bottom_space_settings">
                        @*<label>Master Layout:&nbsp;&nbsp; Note - '{{SubLayout}}' is necessary to render below section items.</label>
                        @Html.TextArea("AddonMasterLayout", new { style="height:150px;width:100%;" })*@
                        <label>Item Layout:&nbsp;&nbsp; Note - '{{Description}}' is necessary.</label>
                        @Html.TextArea("AddonSubLayout", new { style = "height:150px;width:100%;" })
                        @Html.Hidden("addonguid")
                    </li>
                </ul>
            </div>
        }
    </div>
    @helper RenderButtons(Content item)
{
    <a data-addonguid='@item.GUID' class="model-config btn btn-mini btn-default dropdown-toggle" style="padding:2px 6px;">
        <i class="icon-cog" style="margin-top:5px;"></i>
    </a>
    if (Functions.CurrentUserID() == item.AddedBy)
    {
        if (aclService.HasRight(Rights.EditOwnAddons))
        {
            @Html.ActionLink("Edit", "Edit", new { GUID = item.GUID }, new { @class = "btn btn-small btn-blue" })
        }
    }
    else
    {
        if (aclService.HasRight(Rights.EditOtherUsersAddons))
        {
            @Html.ActionLink("Edit", "Edit", new { GUID = item.GUID }, new { @class = "btn btn-small btn-blue" })
        }
    }
    if (Functions.CurrentUserID() == item.AddedBy)
    {
        if (aclService.HasRight(Rights.DeleteOwnAddons))
        {
            @Html.ActionLink("Delete", "Delete", new { GUID = item.GUID }, new { @class = "btn btn-small btn-red", onclick = "return confirm('Are you sure to delete this Page ?');" })
        }
    }
    else
    {
        if (aclService.HasRight(Rights.DeleteOtherUsersAddons))
        {
            @Html.ActionLink("Delete", "Delete", new { GUID = item.GUID }, new { @class = "btn btn-small btn-red", onclick = "return confirm('Are you sure to delete this Page ?');" })
        }
    }
}



@section BottomScript{
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('.model-config').click(function () {
                $('#addonguid').val($(this).attr("data-addonguid"));
                $.ajax({
                    type: "GET",
                    url: '/Admin/Addons/GetAddonLayout',
                    data: { addonguid: $('#addonguid').val() },
                    dataType: "json",
                    success: function (data) {
                        $("#AddonMasterLayout").val(data.MasterLayout);
                        $("#AddonSubLayout").val(data.SubLayout);
                        $('#modal-config').modal('show');
                    },
                    error: function (data) { alert(data); }
                });
            });
        });
    </script>    
}